package com.zzxz.practicaltrainingsys.controller;

import com.zzxz.practicaltrainingsys.entity.Grade;
import com.zzxz.practicaltrainingsys.entity.Major;
import com.zzxz.practicaltrainingsys.service.GradeService;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/grade")
@CrossOrigin
public class GradeController {
    @Autowired
    private GradeService gradeService;

    @GetMapping("/list")
    public List<Grade> list() {
        return gradeService.getGrade();
    }

    // 新增年级接口
    @PostMapping("/insert")
    public int insertGrade(@RequestBody Grade grade) {
        if (grade.getGrade_name() == null || grade.getGrade_name().trim().isEmpty()) {
            return -1;
        }
        boolean success = gradeService.addGrade(grade);
        return success ? 1 : -2;
    }

    //更新
    @PostMapping("/update")
    public int updateGrade(@RequestBody Grade grade) {
        int result = gradeService.updateGrade(grade);
        return result;

    }

    //根据专业编号查找年级名称
    @GetMapping("/findgrade")
    public List<Grade> getGradeByMajorId(@RequestParam Integer major_id) {
        return gradeService.getGradeByMajorId(major_id);
    }


}